package mes.operator.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mes.operator.dto.ComponentDTO;
import mes.operator.dto.EmployeeDTO;
import mes.operator.dto.MachineDTO;
import mes.operator.service.OperatorManageService;
import mes.utility.Utility;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class OperatorController {
	
	private OperatorManageService service;
	
	public OperatorController(OperatorManageService service) {
		this.service = service;
	}
	
	@RequestMapping("/login.do")
	public ModelAndView login(HttpSession session,EmployeeDTO dto){
		ModelAndView mv = new ModelAndView();
		try {
			EmployeeDTO edto=service.login(dto);
			if(edto!= null){
				if(dto.getEmployee_password().equals(edto.getEmployee_password())){
					session.setAttribute("login_info", edto);
						int[] data = {service.getComponentCount("플라스틱 커버(앞면)"), service.getComponentCount("LCD"), service.getComponentCount("칩셋"),
								service.getComponentCount("보드"), service.getComponentCount("플라스틱 커버(뒷면)"), service.getComponentCount("배터리")};
						mv.addObject("minCount", Utility.getMinCount(data));
						mv.addObject("countA", service.getComponentCount("플라스틱 커버(앞면)"));
						mv.addObject("countB", service.getComponentCount("LCD"));
						mv.addObject("countC", service.getComponentCount("칩셋"));
						mv.addObject("countD", service.getComponentCount("보드"));
						mv.addObject("countE", service.getComponentCount("플라스틱 커버(뒷면)"));
						mv.addObject("countF", service.getComponentCount("배터리"));
						mv.setViewName("operator_ready.op");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	} 
	
	@RequestMapping("/loginOut.do")
	public ModelAndView loginOut(HttpSession session){
		ModelAndView mv = new ModelAndView();
		if(session!=null){
			session.invalidate();
			mv.setViewName("login.lo");
		}
		return mv;
	}
	
	@RequestMapping("/startOp.do")
	public ModelAndView startOp(String startProductEA, HttpSession session){
		ModelAndView mv = new ModelAndView();
		int cnt = Integer.parseInt(startProductEA);
		try {
			session.setAttribute("startProductEA", cnt);
			session.setAttribute("componentList_a1", service.getComponentInfo("플라스틱 커버(앞면)", cnt));
			session.setAttribute("componentList_a2", service.getComponentInfo("LCD", cnt));
			session.setAttribute("componentList_b1", service.getComponentInfo("칩셋", cnt));
			session.setAttribute("componentList_b2", service.getComponentInfo("보드", cnt));
			session.setAttribute("componentList_c1", service.getComponentInfo("플라스틱 커버(뒷면)", cnt));
			session.setAttribute("componentList_c2", service.getComponentInfo("배터리", cnt));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		mv.setViewName("operating.op");
		return mv;
	}
	@RequestMapping("/operating_machineA.do")
	public void operating_machineA(HttpSession session, HttpServletRequest request, HttpServletResponse response){
		try{
			int i = Integer.parseInt(request.getParameter("i"));
			System.out.println("A=> i값 : "+i);
			service.setMachineAInfo((ArrayList<ComponentDTO>)session.getAttribute("componentList_a1"), i);
			service.setMachineAInfo((ArrayList<ComponentDTO>)session.getAttribute("componentList_a2"), i);
			PrintWriter out = response.getWriter();
			out.println("<br><img src='/MES_Project/img/1C.gif' id='a' name='a' width='130' height='130' class='machineCss' >");
			out.println(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/operating_machineB.do")
	public void operating_machineB(HttpSession session, HttpServletRequest request, HttpServletResponse response){
		try{
			int i = Integer.parseInt(request.getParameter("i"));
			System.out.println("B=> i값 : "+i);
			service.setMachineBInfo((ArrayList<ComponentDTO>)session.getAttribute("componentList_b1"), i);
			service.setMachineBInfo((ArrayList<ComponentDTO>)session.getAttribute("componentList_b2"), i);
			PrintWriter out = response.getWriter();
			out.println("<img src='/MES_Project/img/2C.gif' id='b' name='b' width='130' height='130' class='machineCss'>" );
			out.println(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/operating_machineC.do")
	public void operating_machineC(HttpSession session, HttpServletRequest request, HttpServletResponse response){
		try{
			int i = Integer.parseInt(request.getParameter("i"));
			System.out.println("C=> i값 : "+i);
			service.setMachineCInfo((ArrayList<ComponentDTO>)session.getAttribute("componentList_c1"), i);
			service.setMachineCInfo((ArrayList<ComponentDTO>)session.getAttribute("componentList_c2"), i);
			PrintWriter out = response.getWriter();
			out.println("<img src='/MES_Project/img/3C.gif' id='c' name='c' width='130' height='130' class='machineCss' >");
			out.println(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/operate_result.do")
	public ModelAndView operate_result(){
		ModelAndView mv = new ModelAndView();
		try {
			mv.addObject("machine_a_size", service.getAllMachineByName("machine_a").size());
			mv.addObject("machine_a_isFault", service.getIsFaultMachineByName("machine_a").size());
			mv.addObject("machine_b_size", service.getAllMachineByName("machine_b").size());
			mv.addObject("machine_b_isFault", service.getIsFaultMachineByName("machine_b").size());
			mv.addObject("machine_c_size", service.getAllMachineByName("machine_c").size());
			mv.addObject("machine_c_isFault", service.getIsFaultMachineByName("machine_c").size());
			mv.setViewName("operate_result.op");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/show_pop.do")
	public ModelAndView showPop(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		String id = request.getParameter("id");
		String count = request.getParameter("count");
		System.out.println("아이디:" + id);
		System.out.println("카운트: " + count);
		String oId = null;
		if(id.equals("a")){
			oId = "machine_a";
		}else if(id.equals("b")){
			oId = "machine_b";
		}else if(id.equals("c")){
			oId = "machine_c";
		}
		try {
			ArrayList mList = service.getAllMachineByName(oId);
			ArrayList sList = new ArrayList();
			for(Object o : mList){
				MachineDTO mdto = (MachineDTO)o;
				System.out.println(mdto.getIs_fault());
				if(mdto.getIs_fault().equals("N")){
					sList.add(mdto);
				}
			}
			
			mv.addObject("id", id);
			mv.addObject("operatingCount", mList.size());
			mv.addObject("successCount", sList.size());
			mv.addObject("fualtCount", mList.size()-sList.size());
//			mv.addObject("resultCount", count-mList.size());
			mv.addObject("count", count);
			mv.setViewName("show_operating");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	@RequestMapping("/reShow_pop.do")
	public void reShowPop(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		HashMap map = new HashMap();
		String id = request.getParameter("id");
		int count = Integer.parseInt(request.getParameter("count"));
		System.out.println("아이디:" + id);
		System.out.println("카운트:" + count);
		String oId = null;
		if(id.equals("a")){
			oId = "machine_a";
		}else if(id.equals("b")){
			oId = "machine_b";
		}else if(id.equals("c")){
			oId = "machine_c";
		}
		try {
			ArrayList mList = service.getAllMachineByName(oId);
			ArrayList sList = new ArrayList();
			for(Object o : mList){
				MachineDTO mdto = (MachineDTO)o;
				System.out.println(mdto);
				if(mdto.getIs_fault().equals("N")){
					sList.add(mdto);
				}
			}
			
			EmployeeDTO edto = (EmployeeDTO) session.getAttribute("login_info");
			JSONObject jobj = new JSONObject();
			jobj.put("name", edto.getEmployee_name());
			jobj.put("department", edto.getEmployee_department());
			jobj.put("id", id);
			jobj.put("operatingCount", mList.size());
			jobj.put("successCount", sList.size());
			jobj.put("fualtCount", mList.size()-sList.size());
			jobj.put("resultCount", count-mList.size());
			jobj.put("count", count);
			
			PrintWriter out = response.getWriter();
			out.println(jobj.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 *  구매페이지로 이동
	 */
	@RequestMapping("/support.do")
	public ModelAndView support() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("support_list.op");
		return mv;
	}
	/**
	 * 구매하기
	 */
	@RequestMapping("/buy.do")
	public ModelAndView support(String plasticF, String lcd, String chipset, String board, String plasticB, String battery) {
		ModelAndView mv = new ModelAndView();
		System.out.println( plasticF +" ," + lcd +" ," +  chipset +" ," +  board +" ," +  plasticB +" ," + battery);
		try{
			if(plasticF != null){
				for(int i =0; i < Integer.parseInt(plasticF); i++){
					service.insertComponentNum("플라스틱 커버(앞면)");
				}
			}
			if(lcd != null){
				for(int i =0; i < Integer.parseInt(lcd); i++){
					service.insertComponentNum("LCD");
				}
			}
			if(chipset != null){
				for(int i =0; i < Integer.parseInt(chipset); i++){
					service.insertComponentNum("칩셋");
				}
			}
			if(board != null){
				for(int i =0; i < Integer.parseInt(board); i++){
					service.insertComponentNum("보드");
				}
			}
			if(plasticB != null){
				for(int i =0; i < Integer.parseInt(plasticB); i++){
					service.insertComponentNum("플라스틱 커버(뒷면)");
				}
			}
			if(battery != null){
				for(int i =0; i < Integer.parseInt(battery); i++){
					service.insertComponentNum("배터리");
				}
			}
			int[] data = {service.getComponentCount("플라스틱 커버(앞면)"), service.getComponentCount("LCD"), service.getComponentCount("칩셋"),
					service.getComponentCount("보드"), service.getComponentCount("플라스틱 커버(뒷면)"), service.getComponentCount("배터리")};
			mv.addObject("minCount", Utility.getMinCount(data));
			mv.addObject("countA", service.getComponentCount("플라스틱 커버(앞면)"));
			mv.addObject("countB", service.getComponentCount("LCD"));
			mv.addObject("countC", service.getComponentCount("칩셋"));
			mv.addObject("countD", service.getComponentCount("보드"));
			mv.addObject("countE", service.getComponentCount("플라스틱 커버(뒷면)"));
			mv.addObject("countF", service.getComponentCount("배터리"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		mv.setViewName("operator_ready.op");
		return mv;
	}
}
